<!--  -->
<template>
  <el-container class="home-container">
    <el-aside :width="isCollapse ? '64px':'200px'">
      <div class="logo"><i class="el-icon-s-platform"></i><span v-if="!isCollapse">项目管理平台</span></div>
      <el-menu
        background-color="#001529"
        text-color="#fff"
        active-text-color="#ffd04b"
        unique-opened
        :collapse="isCollapse"
        router
        :default-active="activePath"
      >
        <!-- 一级菜单 -->
        <el-submenu :index="item.id + ''" v-for="item in menulist.value" :key="item.id">
          <!-- 一级菜单的模板区 -->
          <template #title>
            <!-- 图标 -->
            <i :class="iconsObj[item.id]"></i>
            <!-- 文本 -->
            <span>{{item.authName}}</span>
          </template>
          <!-- 二级菜单 -->
          <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
            <template #title>
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{subItem.authName}}</span>
            </template>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header
        >
        <div class="fold">
            <i :class="isCollapse? 'el-icon-s-unfold':'el-icon-s-fold'" @click="toggleCollapse"></i>
            项目管理平台
        </div>
        <!-- 头部区 -->

        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-main>
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { reactive,ref, toRefs, onBeforeMount, onMounted } from 'vue'
import {ElMessage} from 'element-plus'
import { useRouter } from 'vue-router'
//获取左侧菜单数据
import { getMenuList } from '../../api/index.js'
export default {
  name: '',
  setup() {
    //引入路由
    const router = useRouter()
    //退出登陆按钮
    function logout() {
      window.sessionStorage.clear()
      router.push('/login')
    }

    //获取左侧菜单
    const menulist = reactive([])
    async function getMenulist(){
       const res =  await getMenuList()
       if(res.meta.status !== 200) return Elmessage.error(res.meta.msg)
       menulist.value = res.data
       console.log(menulist)
    }


    //图标对象
    const iconsObj = {
        '125':'el-icon-user-solid',
        '103':'el-icon-s-help',
        '101':'el-icon-s-comment',
        '102':'el-icon-s-order',
        '145':'el-icon-s-data'
    }

    const isCollapse = ref(false)
    //切换菜单折叠与展开
    function toggleCollapse(){
      isCollapse.value = !isCollapse.value
    }

    //被激活的链接地址
    let activePath = ref('')
    //保存链接的激活状态
    const saveNavState = (activePath)=>{
      window.sessionStorage.setItem('activePath',activePath)
      activePath = activePath
    }
    activePath = window.sessionStorage.getItem('activePath')
    onMounted(()=>{
       getMenulist()
    })
    return {
      iconsObj,
      getMenulist,
      menulist,
      logout,
      isCollapse,
      toggleCollapse,
      activePath,
      saveNavState
    }
  }
}
</script>
<style lang="scss" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #fff;
  height: 64px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-aside {
  background-color: #001529;
  .logo {
    height: 60px;
    line-height: 60px;
    text-align: center;
    color: #fff;
    font-size: 18px;
  }
}
.el-main {
  background-color: #eaedf2;
}
.el-menu{
        // width: 100%;
        border-right: none;
}
.fold{
    i{
        font-size: 20px;
        cursor: pointer;
    }
}
</style>
